<?php
namespace app\admin\controller;
use think\Controller;
class Ios extends Controller
{

	// 列表展示
	public function index()
	{
		$field=db('ios')->select();
      	$this->assign("data",$field);
      	return $this->fetch();
	}
	// 添加界面
	public function add() {
		return view();
	}
	public function add_action() {
		$file = request()->file('img');
		$info = $file->move(ROOT_PATH . 'public' . DS . 'static'.DS.'uploads');
		if($info){

			$data['ios_name'] = input('post.name');
			$data['ios_img'] =  $info->getSaveName();
			$data['ios_author'] = input('post.author');
			$data['ios_in_date'] = date('Y-m-d H:i:s');

			if(db('ios')  -> insert($data)) {
				return $this->success('添加成功','admin/ios/index');
			} else {
				return $this->error('添加失败','admin/ios/index');
			}
		}else{
			// 上传失败获取错误信息
			echo $file->getError();
			return $this->error('添加失败','admin/ios/index');
		}
	}
		// 删除界面
	public function del() {

		$where['ios_id'] = input('id');
		if(db("ios")->where($where)->delete()){
			return $this->success('删除成功','admin/ios/index');
		}else{
			return $this->error('删除失败','admin/ios/index');
		}

	}
	/*
	// 修改界面
	 */
	public function edit() {
		$where['ios_id'] = input('id');
		$item = db('ios')  -> where($where)->find();
		$this->assign('item',$item);
		return $this->fetch();
	}
	public function edit_action(){
		$where['ios_id'] = input('id');
		$file = request()->file('img');
		if($file){
			// 移动到框架应用根目录/public/uploads/ 目录下
			$info = $file->move(ROOT_PATH . 'public' . DS . 'static'.DS.'uploads');

			if($where['ios_id'] && $info){

				$data['ios_id'] = $where['ios_id'];
				$data['ios_name'] = input('post.name');
				$data['ios_img'] =   $info->getSaveName();
				$data['ios_author'] = input('post.author');
				$data['ios_in_date'] = date('Y-m-d H:i:s');

				if(db('ios')  -> update($data)) {
					return $this->success('修改成功','admin/ios/index');
				} else {
					return $this->error('修改失败','admin/ios/index');
				}
			}
			else{
				return $this->error('修改失败','admin/index/index');
			}
		}
	}


}
